!include "mui2.nsh"
InstType "Full"
InstType "Lite"
InstType "Minimal"
!include "MUI2.nsh"
!include "Sections.nsh"
!include "LogicLib.nsh"
!include "Memento.nsh"
!include "WordFunc.nsh"

#!define QTDIR "D:\Qt\4.7.0"
!define PRODUCT_NAME "Test"
!define PRODUCT_VERSION "1.0.0.1"
Var INSTALL_DIR
Var LANGUAGE_SELECT

SetCompressor lzma

outfile "Setup${PRODUCT_NAME}_${PRODUCT_VERSION}.exe"
installDir "$PROGRAMFILES\Test" 
XPStyle on
RequestExecutionLevel admin

Page directory beforeDirectoryPage "" afterDirectoryPage
Page instfiles
;!insertmacro MUI_PAGE_INSTFILES
;!insertmacro MUI_LANGUAGE "SimpChinese"


LoadLanguageFile "Unicode_NSIS\Contrib\Language files\English.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\German.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Japanese.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\French.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Italian.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Spanish.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Portuguese.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Danish.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Dutch.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Russian.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\Arabic.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\SimpChinese.nlf"
LoadLanguageFile "Unicode_NSIS\Contrib\Language files\TradChinese.nlf"

Function beforeDirectoryPage
	ReadRegStr $0 HKCU "Software\Test" "installDirectory"
	${If} $0 == ""
		;no exit directory page, do nothing
	${Else}
		;exit directory page
		Abort
	${EndIf}
FunctionEnd

Function afterDirectoryPage
	ReadRegStr $0 HKCU "Software\Test" "installDirectory"
	${If} $0 == ""
		;set install directory to reg
		WriteRegStr HKCU "Software\Test" "installDirectory" $INSTDIR
		StrCpy $INSTALL_DIR $INSTDIR
	${Else}
		;exit directory page and get last install dir
		StrCpy $INSTALL_DIR $0
		Abort
	${EndIf}
FunctionEnd

Function .onInit
	;Language selection dialog
	Push ""
	Push ${LANG_ENGLISH}
	Push English
	Push ${LANG_GERMAN}
	Push German
	Push ${LANG_JAPANESE}
	Push Japanese
	Push ${LANG_FRENCH}
	Push French
	Push ${LANG_ITALIAN}
	Push Italian
	Push ${LANG_SPANISH}
	Push Spanish
	Push ${LANG_PORTUGUESE}
	Push Portuguese
	Push ${LANG_DANISH}
	Push Danish
	Push ${LANG_DUTCH}
	Push Dutch
	Push ${LANG_RUSSIAN}
	Push Russian
	Push ${LANG_ARABIC}
	Push Arabic
	Push ${LANG_TRADCHINESE}
	Push "Traditional Chinese"
	Push ${LANG_SIMPCHINESE}
	Push "Simplified Chinese"
	Push A ; A means auto count languages
	       ; for the auto count to work the first empty push (Push "") must remain
	LangDLL::LangDialog "Installer Language" "Please select the language of the installer"

	Pop $LANGUAGE
	
	StrCmp $LANGUAGE ${LANG_ENGLISH} 0 +2
		StrCpy $LANGUAGE_SELECT "English"
	StrCmp $LANGUAGE ${LANG_GERMAN} 0 +2
		StrCpy $LANGUAGE_SELECT "German"
	StrCmp $LANGUAGE ${LANG_JAPANESE} 0 +2
		StrCpy $LANGUAGE_SELECT "Japanese"
	StrCmp $LANGUAGE ${LANG_FRENCH} 0 +2
		StrCpy $LANGUAGE_SELECT "French"
	StrCmp $LANGUAGE ${LANG_ITALIAN} 0 +2
		StrCpy $LANGUAGE_SELECT "Italian"
	StrCmp $LANGUAGE ${LANG_SPANISH} 0 +2
		StrCpy $LANGUAGE_SELECT "Spanish"
	StrCmp $LANGUAGE ${LANG_PORTUGUESE} 0 +2
		StrCpy $LANGUAGE_SELECT "Portuguese"
	StrCmp $LANGUAGE ${LANG_DANISH} 0 +2
		StrCpy $LANGUAGE_SELECT "Danish"
	StrCmp $LANGUAGE ${LANG_DUTCH} 0 +2
		StrCpy $LANGUAGE_SELECT "Dutch"
	StrCmp $LANGUAGE ${LANG_RUSSIAN} 0 +2
		StrCpy $LANGUAGE_SELECT "Russian"
	StrCmp $LANGUAGE ${LANG_ARABIC} 0 +2
		StrCpy $LANGUAGE_SELECT "Arabic"
	StrCmp $LANGUAGE ${LANG_TRADCHINESE} 0 +2
		StrCpy $LANGUAGE_SELECT "Traditional Chinese"
	StrCmp $LANGUAGE ${LANG_SIMPCHINESE} 0 +2
		StrCpy $LANGUAGE_SELECT "Simplified Chinese"
	 
	WriteRegStr HKCU "Software\Test" "language" $LANGUAGE_SELECT
	
	StrCmp $LANGUAGE "cancel" 0 +2
		Abort
FunctionEnd

section
ReadRegStr $0 HKCU "Software\Test" "installDirectory"
StrCpy $INSTALL_DIR $0

setOutPath $INSTALL_DIR
file Dealsofts\conf.dll
file Dealsofts\libgcc_s_dw2-1.dll
file Dealsofts\libstdc++-6.dll
file Dealsofts\mingwm10.dll
file Dealsofts\msvcp100.dll
file Dealsofts\msvcr100.dll
file Dealsofts\presets.xml
file Dealsofts\QtCore4.dll
file Dealsofts\QtGui4.dll
file Dealsofts\QtNetwork4.dll
file Dealsofts\QtWebKit4.dll
file Dealsofts\dealsofts.exe

;install ffmpeg, 10 files
;file Dealsofts\avcodec-55.dll
;file Dealsofts\avdevice-55.dll
;file Dealsofts\avfilter-4.dll
;file Dealsofts\avformat-55.dll
;file Dealsofts\avutil-52.dll
;file Dealsofts\postproc-52.dll
;file Dealsofts\swresample-0.dll
;file Dealsofts\swscale-2.dll
;file Dealsofts\avconv
;file Dealsofts\avprobe
file Dealsofts\avconv.exe
file Dealsofts\avprobe.exe

setOutPath $INSTALL_DIR\config
file Dealsofts\config\Option.ini

setOutPath $INSTALL_DIR\plugins\codecs
file Dealsofts\plugins\codecs\qcncodecs4.dll
file Dealsofts\plugins\codecs\qcncodecsd4.dll
file Dealsofts\plugins\codecs\qjpcodecs4.dll
file Dealsofts\plugins\codecs\qjpcodecsd4.dll
file Dealsofts\plugins\codecs\qkrcodecs4.dll
file Dealsofts\plugins\codecs\qkrcodecsd4.dll
file Dealsofts\plugins\codecs\qtwcodecs4.dll
file Dealsofts\plugins\codecs\qtwcodecsd4.dll

setOutPath $INSTALL_DIR\plugins\iconengines
file Dealsofts\plugins\iconengines\qsvgicon4.dll
file Dealsofts\plugins\iconengines\qsvgicond4.dll

setOutPath $INSTALL_DIR\plugins\imageformats
file Dealsofts\plugins\imageformats\qgif4.dll
file Dealsofts\plugins\imageformats\qgifd4.dll
file Dealsofts\plugins\imageformats\qico4.dll
file Dealsofts\plugins\imageformats\qicod4.dll
file Dealsofts\plugins\imageformats\qjpeg4.dll
file Dealsofts\plugins\imageformats\qjpegd4.dll
file Dealsofts\plugins\imageformats\qmng4.dll
file Dealsofts\plugins\imageformats\qmngd4.dll
file Dealsofts\plugins\imageformats\qsvg4.dll
file Dealsofts\plugins\imageformats\qsvgd4.dll
file Dealsofts\plugins\imageformats\qtga4.dll
file Dealsofts\plugins\imageformats\qtgad4.dll
file Dealsofts\plugins\imageformats\qtiff4.dll
file Dealsofts\plugins\imageformats\qtiffd4.dll

setOutPath $INSTALL_DIR\translations
file Dealsofts\translations\01qwinff_zh.qm
file Dealsofts\translations\02qwinff_zh_tw.qm
file Dealsofts\translations\03qwinff_en.qm
file Dealsofts\translations\04qwinff_de.qm
file Dealsofts\translations\05qwinff_ja.qm
file Dealsofts\translations\06qwinff_fr.qm
file Dealsofts\translations\07qwinff_it.qm
file Dealsofts\translations\08qwinff_es.qm
file Dealsofts\translations\09qwinff_pt.qm
file Dealsofts\translations\10qwinff_da.qm
file Dealsofts\translations\11qwinff_nl.qm
file Dealsofts\translations\12qwinff_ru.qm
file Dealsofts\translations\13qwinff_pt_br.qm
file Dealsofts\translations\14qwinff_vi.qm
file Dealsofts\translations\15qwinff_ar.qm
file Dealsofts\translations\16qwinff_pl.qm
file Dealsofts\translations\17qwinff_ko.qm

writeUninstaller $INSTALL_DIR\uninstaller.exe

createShortCut "$DESKTOP\Test.lnk" "$INSTALL_DIR\dealsofts.exe"

CreateDirectory "$SMPROGRAMS\Test"
createShortCut "$SMPROGRAMS\Test\Test.lnk" "$INSTALL_DIR\dealsofts.exe"
createShortCut "$SMPROGRAMS\Test\uninstaller.lnk" "$INSTALL_DIR\uninstaller.exe"
ExecShell "open" "http://www.dealsofts.com/free_video_converter/install.html"
sectionEnd

section "Uninstall"
ReadRegStr $0 HKCU "Software\Test" "installDirectory"
StrCpy $INSTALL_DIR $0
rmDir /r "$INSTALL_DIR"
delete "$DESKTOP\Test.lnk"
delete "$SMPROGRAMS\Test\Test.lnk"
delete "$SMPROGRAMS\Test\uninstaller.lnk"
rmDir /r "$SMPROGRAMS\Test"
DeleteRegKey /ifempty HKCU "Software\Test"
ExecShell "open" "http://www.dealsofts.com/free_video_converter/uninstall.html"
sectionEnd